package org.gten.gegedexin.service;

import org.gten.gegedexin.Data.Mail;
import org.gten.gegedexin.SMTP.SSLEmailSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class SendEmail {
    @Autowired
    MailInfoServiceImpl mailInfoService;
    @Autowired
    TokenServiceImpl tokenService;

    SSLEmailSender smtp = new SSLEmailSender();

    public int sendmail(Mail mail) throws Exception {
//        用SMTP将邮件送出
//        给邮件分配id，同时将邮件信息加入数据库中
        String token = tokenService.getAccount(mail.getSender());
//        String token = "123";
        String data = LocalDateTime.now().toString();
        int id = mailInfoService.addMailInfo(mail.getTitle(),token,mail.getReceiver(),data);
        String mailId = String.format("%08d",id);
        smtp.sendText(mailId+mail.getTitle(),mail.getText());
        return id;
    }
}
